package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(value = "用户地址")
public class UserAddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    @ApiOperation(value = "添加地址")
    public Result saveAddress(@RequestBody AddressBook addressBook) {
        Integer integer = addressBookService.saveAddress(addressBook);
        if (integer == null || integer == 0) {
            return Result.error("插入失败");
        } else {
            return Result.success();
        }
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询用户地址")
    public Result list() {
        return Result.success(addressBookService.seleteAddress());
    }

    @GetMapping("/default")
    @ApiOperation(value = "查询默认地址")
    public Result defaultAddress() {
        return Result.success(addressBookService.selectDefaultAddress());
    }

    @PutMapping("/default")
    @ApiOperation(value = "更改默认地址")
    public Result updateDefaultAddress( @RequestBody Map<String,Long> map) {
        Long id =map.get(map.keySet().toArray()[0]);
        addressBookService.updateDefaultAddress(id);
        return Result.success();
    }

    @PutMapping
    @ApiOperation(value = "更改地址")
    public Result updateAddress(@RequestBody AddressBook addressBook) {
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询地址")
    public Result<AddressBook> selectAddress(@PathVariable Long id) {
        return Result.success(addressBookService.selectAddressById(id));
    }

    @DeleteMapping
    @ApiOperation(value = "删除地址")
    public Result deleteAddress(@RequestParam Long id) {
        addressBookService.deleteAddressById(id);
        return Result.success();
    }

}







